﻿using System;
using System.Windows;
using System.Windows.Controls;

using Hetymine.Executioner.Commands;

namespace Hetymine.Executioner.Plugins.FTP {
    /// <summary>
    /// Interaction logic for FTPCommandConfig.xaml
    /// </summary>
    public partial class FTPCommandConfig : UserControl, ICommandConfigurator<ICommand> {
        private bool _IsApply = false;
        private FTPCommand _FTPCommand;

        public FTPCommandConfig() {
            InitializeComponent();

            this.cmdName.TextChanged += new TextChangedEventHandler(cmdName_TextChanged);
            this.cmdHost.TextChanged += new TextChangedEventHandler(cmdHost_TextChanged);
            this.cmdRoot.TextChanged += new TextChangedEventHandler(cmdRoot_TextChanged);
            this.cmdPassive.Click += new RoutedEventHandler(cmdPassive_Click);
            this.cmdUsername.TextChanged += new TextChangedEventHandler(cmdUsername_TextChanged);
            this.cmdPassword.PasswordChanged += new RoutedEventHandler(cmdPassword_PasswordChanged);
            this.cmdFileDirectory.TextChanged += new TextChangedEventHandler(cmdFileDirectory_TextChanged);
            this.cmdCopy.Click += new RoutedEventHandler(cmdCopy_Click);
        }

        private string CommandName {
            get {
                if (this._FTPCommand.IsEmpty) {
                    return string.Empty;
                }

                return this._FTPCommand.ToString();
            }
        }

        private void cmdName_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply || this._FTPCommand == null) { return; }

            this._FTPCommand.Name = this.cmdName.Text.Trim();

            this.OnCommandNameChanged(this, new CommandNameEventArgs(this.CommandName));
        }
        private void cmdHost_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply || this._FTPCommand == null) { return; }

            this._FTPCommand.Host = this.cmdHost.Text.Trim();

            this.OnCommandNameChanged(this, new CommandNameEventArgs(this.CommandName));
        }
        private void cmdRoot_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply || this._FTPCommand == null) { return; }

            this._FTPCommand.RootDirectory = this.cmdRoot.Text.Trim();

            this.OnCommandNameChanged(this, new CommandNameEventArgs(this.CommandName));
        }
        private void cmdPassive_Click(object sender, RoutedEventArgs e) {
            if (!this._IsApply || this._FTPCommand == null) { return; }

            this._FTPCommand.Passive = this.cmdPassive.IsChecked.Value;

            this.OnCommandNameChanged(this, new CommandNameEventArgs(this.CommandName));
        }
        private void cmdUsername_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply || this._FTPCommand == null) { return; }

            this._FTPCommand.Username = this.cmdUsername.Text.Trim();

            this.OnCommandNameChanged(this, new CommandNameEventArgs(this.CommandName));
        }
        private void cmdPassword_PasswordChanged(object sender, RoutedEventArgs e) {
            if (!this._IsApply || this._FTPCommand == null) { return; }

            this._FTPCommand.Password = this.cmdPassword.Password;

            this.OnCommandNameChanged(this, new CommandNameEventArgs(this.CommandName));
        }
        private void cmdFileDirectory_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply || this._FTPCommand == null) { return; }

            this._FTPCommand.FileDirectory = this.cmdFileDirectory.Text.Trim();

            this.OnCommandNameChanged(this, new CommandNameEventArgs(this.CommandName));
        }
        private void cmdCopy_Click(object sender, RoutedEventArgs e) {
            if (!this._IsApply || this._FTPCommand == null) { return; }

            this._FTPCommand.Copy = this.cmdCopy.IsChecked.Value;

            this.OnCommandNameChanged(this, new CommandNameEventArgs(this.CommandName));
        }

        #region ICommandConfigurator<ICommand> Members
        public void InitializeConfig(ICommand config) {
            this._IsApply = false;

            if (config == null) {
                this._FTPCommand = null;

                this.cmdName.Text = string.Empty;
                this.cmdHost.Text = string.Empty;
                this.cmdRoot.Text = string.Empty;
                this.cmdPassive.IsChecked = false;
                this.cmdUsername.Text = string.Empty;
                this.cmdPassword.Password = string.Empty;
                this.cmdFileDirectory.Text = string.Empty;
                this.cmdCopy.IsChecked = false;
            }
            else {
                this._FTPCommand = config as FTPCommand;

                this.cmdName.Text = this._FTPCommand.Name;
                this.cmdHost.Text = this._FTPCommand.Host;
                this.cmdRoot.Text = this._FTPCommand.RootDirectory;
                this.cmdPassive.IsChecked = this._FTPCommand.Passive;
                this.cmdUsername.Text = this._FTPCommand.Username;
                this.cmdPassword.Password = this._FTPCommand.Password;
                this.cmdFileDirectory.Text = this._FTPCommand.FileDirectory;
                this.cmdCopy.IsChecked = this._FTPCommand.Copy;
            }

            this._IsApply = true;
        }

        public UserControl ConfigPanel { get { return this; } }

        public event CommandNameEventHandler CommandNameChanged;
        protected virtual void OnCommandNameChanged(object sender, CommandNameEventArgs e) {
            if (this.CommandNameChanged != null) {
                this.CommandNameChanged(sender, e);
            }
        }
        #endregion
    }
}
